#!/bin/bash

# 服务器管理脚本
# 使用方法: ./server-manage.sh [命令] [服务器IP] [用户名]

COMMAND=${1:-status}
SERVER_IP=${2:-14.103.180.56}
USERNAME=${3:-root}
APP_NAME="warp-knitting-monitor"
REMOTE_DIR="/opt/$APP_NAME"

case $COMMAND in
    "status")
        echo "检查应用状态..."
        ssh $USERNAME@$SERVER_IP << EOF
            echo "=== 应用状态 ==="
            if [ -f $REMOTE_DIR/app.pid ]; then
                PID=\$(cat $REMOTE_DIR/app.pid)
                if ps -p \$PID > /dev/null; then
                    echo "应用正在运行，PID: \$PID"
                    echo "端口8081状态:"
                    netstat -tlnp | grep :8081 || echo "端口8081未监听"
                else
                    echo "应用未运行（PID文件存在但进程不存在）"
                fi
            else
                echo "应用未运行（无PID文件）"
            fi
            
            echo ""
            echo "=== Docker容器状态 ==="
            docker ps | grep $APP_NAME || echo "未找到相关Docker容器"
            
            echo ""
            echo "=== 系统资源 ==="
            echo "内存使用:"
            free -h
            echo ""
            echo "磁盘使用:"
            df -h | grep -E "(Filesystem|/opt)"
        EOF
        ;;
    
    "start")
        echo "启动应用..."
        ssh $USERNAME@$SERVER_IP << EOF
            cd $REMOTE_DIR
            if [ -f start-app.sh ]; then
                ./start-app.sh
            else
                echo "启动脚本不存在，尝试使用Docker启动..."
                docker-compose up -d
            fi
        EOF
        ;;
    
    "stop")
        echo "停止应用..."
        ssh $USERNAME@$SERVER_IP << EOF
            cd $REMOTE_DIR
            if [ -f app.pid ]; then
                kill \$(cat app.pid) 2>/dev/null || true
                rm -f app.pid
                echo "应用已停止"
            fi
            docker-compose down 2>/dev/null || true
            echo "Docker容器已停止"
        EOF
        ;;
    
    "restart")
        echo "重启应用..."
        ssh $USERNAME@$SERVER_IP << EOF
            cd $REMOTE_DIR
            # 停止应用
            if [ -f app.pid ]; then
                kill \$(cat app.pid) 2>/dev/null || true
                rm -f app.pid
            fi
            docker-compose down 2>/dev/null || true
            
            # 启动应用
            if [ -f start-app.sh ]; then
                ./start-app.sh
            else
                docker-compose up -d
            fi
        EOF
        ;;
    
    "logs")
        echo "查看应用日志..."
        ssh $USERNAME@$SERVER_IP << EOF
            echo "=== 应用日志 ==="
            if [ -f $REMOTE_DIR/app.log ]; then
                tail -50 $REMOTE_DIR/app.log
            else
                echo "应用日志文件不存在"
            fi
            
            echo ""
            echo "=== Docker容器日志 ==="
            docker logs --tail 50 $APP_NAME 2>/dev/null || echo "Docker容器日志不存在"
        EOF
        ;;
    
    "tail")
        echo "实时查看日志..."
        ssh $USERNAME@$SERVER_IP "tail -f $REMOTE_DIR/app.log"
        ;;
    
    "update")
        echo "更新应用..."
        ./deploy.sh $SERVER_IP $USERNAME
        ;;
    
    "update-docker")
        echo "更新Docker应用..."
        ./deploy-docker.sh $SERVER_IP $USERNAME
        ;;
    
    "clean")
        echo "清理应用..."
        ssh $USERNAME@$SERVER_IP << EOF
            cd $REMOTE_DIR
            # 停止应用
            if [ -f app.pid ]; then
                kill \$(cat app.pid) 2>/dev/null || true
                rm -f app.pid
            fi
            docker-compose down 2>/dev/null || true
            docker rmi $APP_NAME 2>/dev/null || true
            
            # 清理日志
            rm -f app.log
            rm -rf logs/*
            
            echo "应用已清理"
        EOF
        ;;
    
    *)
        echo "使用方法: $0 [命令] [服务器IP] [用户名]"
        echo "可用命令:"
        echo "  status      - 查看应用状态"
        echo "  start       - 启动应用"
        echo "  stop        - 停止应用"
        echo "  restart     - 重启应用"
        echo "  logs        - 查看日志"
        echo "  tail        - 实时查看日志"
        echo "  update      - 更新应用"
        echo "  update-docker - 更新Docker应用"
        echo "  clean       - 清理应用"
        ;;
esac 